<template>
  <div class="app-container ">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['enterprise:process:add']"
          icon="el-icon-plus"
          plain
          size="small"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['enterprise:process:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="small"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['enterprise:process:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="small"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['enterprise:process:export']"
          :loading="exportLoading"
          icon="el-icon-download"
          plain
          size="small"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="processList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
<!--      <el-table-column align="center" label="企业名称" data-mode="remark"/>-->
      <el-table-column align="center" label="生产工艺名称" prop="psName"/>
      <el-table-column align="center" label="用电量" prop="psElectricity"/>
      <el-table-column align="center" label="用水量" prop="psWater"/>
<!--      <el-table-column align="center" label="废水产生量" prop="psWasteWater"/>-->
<!--      <el-table-column align="center" label="主要污染物产生量" prop="psMasterPollution"/>
      <el-table-column align="center" label="主要污染物排放去向" prop="psMasterPollutionDirection"/>-->
      <el-table-column align="center" label="生产工艺流程图" prop="psFlowChat">
        <template slot-scope="scope" v-if="scope.row.psFlowChat">
          <el-popover placement="right" title="" trigger="hover">
            <img :src="`${baseUrl}${scope.row.psFlowChat}`"  style="height: 500px;width: 500px"/>
            <img slot="reference" :src="`${baseUrl}${scope.row.psFlowChat}`"  :alt="scope.row.psFlowChat" style="height: 100px;width: 150px">
          </el-popover>
        </template>
      </el-table-column>
      <!--      <el-table-column align="center" label="停产申请" prop="psStopProApplication"/>-->
      <!--      <el-table-column align="center" label="停产时间段" prop="psStopProTime"/>-->
      <!--      <el-table-column align="center" label="锅炉建设时间" prop="psBoilerProTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.psBoilerProTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column align="center" label="锅炉燃料方式" prop="psBoilerType"/>-->
      <!--      <el-table-column align="center" label="企业统一信用代码" prop="epId"/>-->
      <!--      <el-table-column align="center" label="备注" prop="remark"/>-->
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['enterprise:process:edit']"
            size="small"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['enterprise:process:remove']"
            size="small"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改生产工艺对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body class="app-container-process" width="80%">

      <el-form ref="form" :model="form" :rules="rules" class="clearfix " label-width="140px">
        <div class="content-left" style="margin-left: 10%">
          <div class="title2"><span> </span>基本信息</div>
          <el-form-item label="生产工艺名称" prop="psName">
            <el-input v-model="form.psName" placeholder="请输入生产工艺名称"/>
          </el-form-item>
          <el-form-item label="经纬度">
            <el-row>
              <el-col>
                <div class="weidu" style="display:flex;">
                  <div style="width: 40%">
                    <el-input v-model="form.psLg" placeholder="请输入经度"/>
                  </div>
                  <div style="width: 40%">
                    <el-input v-model="form.psLt" placeholder="请输入维度"/>
                  </div>
                  <!--                  <div style="width: 20%; text-align: center">坐标</div>-->
                </div>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="原(辅)材料和用量">
            <el-row>
              <el-col>
                <div class="weidu" style="display:flex;">
                  <div style="width: 35%">
                    <el-input v-model="form.psType" placeholder="原(辅)材料"/>
                  </div>
                  <div style="width: 35%">
                    <el-input v-model="form.psDosage" placeholder="用量"/>
                  </div>
                  <el-button style="width: 30%; text-align:  center" type="primary" @click="add"  class="edit-add">

                    <span v-if="form.psType && form.psDosage" @click="editMore "> 编辑</span>
                    <span  v-else>添加更多</span>
                  </el-button>
                </div>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="主要污染物及去向">
            <el-row>
              <el-col>
                <div class="weidu" style="display:flex;">
                  <div style="width: 35%">
                    <el-input v-model="form.psMasterPollution" placeholder="主要污染物"/>
                  </div>
                  <div style="width: 35%">
                    <el-input v-model="form.psMasterPollutionDirection" placeholder="主要污染物去向"/>
                  </div>
                  <el-button style="width: 30%; text-align:  center" type="primary" @click="wry_add"  class="edit-add">

                    <span v-if="form.psMasterPollution && form.psMasterPollutionDirection" @click="wry_editMore "> 编辑</span>
                    <span v-else>添加更多</span>
                  </el-button>
                </div>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="能源消耗情况" prop="psEnergyConsumption">
            <el-input v-model="form.psEnergyConsumption" placeholder="请输入能源消耗情况"/>
          </el-form-item>
          <el-form-item label="年用水量(吨)" prop="psWater">
            <el-input v-model="form.psWater" placeholder="请输入用水量"/>
          </el-form-item>
          <el-form-item label="年用电量(千瓦)" prop="psElectricity">
            <el-input v-model="form.psElectricity" placeholder="请输入用电量"/>
          </el-form-item>
<!--          <el-form-item label="主要污染物产生量" prop="psMasterPollution">
            <el-input v-model="form.psMasterPollution" placeholder="请输入主要污染物产生量"/>
          </el-form-item>
          <el-form-item label="主要污染物去向" prop="psMasterPollutionDirection">
            <el-input v-model="form.psMasterPollutionDirection" placeholder="请输入主要污染物"/>
          </el-form-item>-->

        </div>
        <div class="content-middle" style="margin-left: 15%">
          <div class="title2"><span> </span>设施信息</div>
          <el-form-item label="生产工艺流程图" prop="psFlowChat">
              <ImageUpload ref= "image" v-model="form.psFlowChat"/>
            <!-- <el-input v-model="form.psFlowChat" placeholder="请输入生产工艺流程图" /> -->
          </el-form-item>
          <div class="title2"><span> </span>生产设施运行记录</div>
          <el-form-item label="停产申请文件上传" prop="psStopProApplication">
            <FileUpload v-model="form.psStopProApplication"/>
          </el-form-item>
          <el-form-item label="停产时间段" prop="psStopProTime">
            <el-date-picker type="daterange" v-model="form.psStopProTime" format="yyyy-MM-dd"
                            value-format="yyyy-MM-dd" :style="{width: '100%'}" start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            range-separator="至" clearable  @change="dateChange1">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="是否有锅炉" prop="psWasteWater">
            <el-select v-model="form.psWasteWater" placeholder="请选择" @change="changeOK">
              <el-option
                v-for="dict in typeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
            <!--            <el-input v-model="form.psWasteWater" placeholder="请输入废水产生量"/>-->
          </el-form-item>
          <div v-show="guolu">
          <el-form-item label="锅炉建设时间" prop="psBoilerProTime">
            <el-date-picker v-model="form.psBoilerProTime" clearable
                            placeholder="选择锅炉建设时间"
                            size="small"
                            style="width:100%"
                            type="date"
                            value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="燃料种类" prop="psBoilerType">
            <el-select v-model="form.psBoilerType" placeholder="请选择">
              <el-option
                v-for="dict in typeOptions_rlzl"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
          </div>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer tianjie_button" style="text-align: center;">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog   :visible.sync="open2" width="40%" >
      <div>
          <div v-for="(item,index) in itemPerson" class="fuliao " style="  margin: 0 auto; margin-bottom: 10px">
             <div class="ful-input">
                 <div class="ful-input-box">
                 <span>原辅材料</span>
                 <el-input v-model="item.x1" style="height: 30px;"></el-input>
               </div>
                <div  class="ful-input-box">
              <span>用量</span>
              <el-input  v-model="item.x2"></el-input>
            </div>
             </div>

           <div  class="ful-btn">
             <el-button type="primary" @click="addMore(index)" style="height: 30px; line-height: 30px;padding: 0;padding-left: 15px; padding-right: 15px">添加</el-button>
             <el-button type="danger" @click="removeMore(index)"  style="height: 30px; line-height: 30px;padding: 0;padding-left: 15px; padding-right: 15px">删除</el-button>
           </div>
          </div>
        <div slot="footer" class="dialog-footer tianjie_button" style="text-align: center;">
          <el-button type="primary" @click="btnMore">确 定</el-button>
          <el-button @click="btnMoreClose">取 消</el-button>
        </div>
      </div>

    </el-dialog>
    <el-dialog   :visible.sync="open3" width="40%" >
      <div>
        <div v-for="(item,index) in itemWry" class="fuliao " style="  margin: 0 auto; margin-bottom: 10px">
          <div class="ful-input">
            <div class="ful-input-box">
              <span>名称</span>
              <el-input v-model="item.x1" style="height: 30px;"></el-input>
            </div>
            <div  class="ful-input-box">
              <span>去向</span>
              <el-input  v-model="item.x2"></el-input>
            </div>
          </div>

          <div  class="ful-btn">
            <el-button type="primary" @click="wry_addMore(index)" style="height: 30px; line-height: 30px;padding: 0;padding-left: 15px; padding-right: 15px">添加</el-button>
            <el-button type="danger" @click="wry_removeMore(index)"  style="height: 30px; line-height: 30px;padding: 0;padding-left: 15px; padding-right: 15px">删除</el-button>
          </div>
        </div>
        <div slot="footer" class="dialog-footer tianjie_button" style="text-align: center;">
          <el-button type="primary" @click="wry_btnMore">确 定</el-button>
          <el-button @click="wry_btnMoreClose">取 消</el-button>
        </div>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import {addProcess, delProcess, exportProcess, getProcess, listProcess, updateProcess} from "@/api/enterprise/process";
import {InfoItem} from "@/api/enterprise/info";
import {isIntegerNotMust,validatePhoneTwo,validateIdNo,validateEMail} from "@/api/validate/validate";

export default {
  name: "Process",
  data() {
    return {
      guolu:false,
      itemPerson:[
         {
           x1:"",
           x2:""
         }
       ],
      itemWry:[
        {
          x1:"",
          x2:""
        }
      ],
      open2:false,
      open3:false,
      baseUrl: process.env.VUE_APP_BASE_API,
      glrl: [
        {
          value: '0',
          label: '层状燃烧'
        },
        {
          value: '1',
          label: '悬浮燃烧'
        },
        {
          value: "2",
          label: '沸腾燃烧'
        },
        {
          value: "3",
          label: '气化燃烧'
        }
      ],
      glrlIndex: '0',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      value: '',
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产工艺表格数据
      processList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        psName: null,
        psLg: null,
        psLt: null,
        psType: null,
        psDosage: null,
        psEnergyConsumption: null,
        psElectricity: null,
        psWater: null,
        psWasteWater: null,
        psMasterPollution: null,
        psMasterPollutionDirection: null,
        psFlowChat: null,
        psStopProApplication: null,
        psStopProTime: null,
        psBoilerProTime: null,
        psBoilerType: null,
        epId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        psName: [
          {required: true, message: "生产工艺名称不能为空", trigger: "blur"}
        ],
      },
      typeOptions:[],
      typeOptions_rlzl:[]
  };
  },
  created() {
    this.getDicts("common_whether").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("rlzl").then(response => {
      this.typeOptions_rlzl = response.data;
    });
    this.getList();
  },
  methods: {
    editMore(){
      var X1,X2;
        if( this.form.psType){
           X1=this.form.psType.split(",")
        }
        if(  this.form.psDosage){
           X2=this.form.psDosage.split(",")
        }
      var arr=[];
      for(var i=0;i<X1.length;i++){
        arr.push({
              x1:X1[i],
              x2:X2[i]
           })
      }
      this.itemPerson=arr;


    },
    btnMoreClose(){
        this.open2=false;
    },
    btnMore(){
      var arrx1=[];
      var  arrX2=[]
      var stautus;
      var that = this
      this.itemPerson.map(function(item){
        if(item.x1 && item.x2){
          arrx1.push(item.x1);
          arrX2.push(item.x2);
          stautus = true;
          that.open2=false;
        }else{
          stautus = false;
        }
      })
      this.form.psType=arrx1.join(',')
      this.form.psDosage=arrX2.join(',');
      return stautus;
    },
    removeMore(index){
      if( this.itemPerson.length<=1){
        this.msgError("至少保留一行");
        return;
      }
      this.itemPerson.splice(index, 1)
    },
    add(){
        this.open2=true;
    },
    addMore(index){
     /* var end=this.itemPerson.length-1;
      if( this.itemPerson[end].x1=="" || this.itemPerson[end].x2==""){
        this.msgError("当前行不能为空");
        return;
      }*/
      this.itemPerson.push( {
        x1:"",
        x2:""
      })
},




    wry_editMore(){
      var X1,X2;
      if( this.form.psMasterPollution){
        X1=this.form.psMasterPollution.split(",")
      }
      if(this.form.psMasterPollutionDirection){
        X2=this.form.psMasterPollutionDirection.split(",")
      }
      var arr=[];
      for(var i=0;i<X1.length;i++){
        arr.push({
          x1:X1[i],
          x2:X2[i]
        })
      }
      this.itemWry=arr;
    },
    wry_btnMoreClose(){
      this.open3=false;
    },
    wry_btnMore(){
      var arrx1=[];
      var  arrX2=[]
      var stautus;
      var that = this
      this.itemWry.map(function(item){
        if(item.x1 && item.x2){
          arrx1.push(item.x1);
          arrX2.push(item.x2);
          that.form.psMasterPollution=arrx1.join(',')
          that.form.psMasterPollutionDirection=arrX2.join(',');
          stautus = true;
          that.open3=false;
        }else{
          stautus = false;
        }
      })
      return stautus;
    },
    wry_removeMore(index){
      if( this.itemWry.length<=1){
        this.msgError("至少保留一行");
        return;
      }
      this.itemWry.splice(index, 1)
    },
    wry_add(){
      this.open3=true;
    },
    wry_addMore(index){
      /* var end=this.itemPerson.length-1;
       if( this.itemPerson[end].x1=="" || this.itemPerson[end].x2==""){
         this.msgError("当前行不能为空");
         return;
       }*/
      this.itemWry.push( {
        x1:"",
        x2:""
      })
    },



    changeOK(e){
      if(e == 0){
        this.guolu = true;
      }
      if(e == 1){
        this.guolu = false;
      }
    },
    dateChange1(data){
      this.form.psStopProTime = data.join(',')
    },
    /** 查询生产工艺列表 */
    getList() {
      this.loading = true;
      const username = this.$store.state.user.name;
      InfoItem(username).then(res => {
        if (res.hasOwnProperty("data")) {
          this.queryParams.epId = res.data.epId;
          listProcess(this.queryParams).then(response => {
            this.processList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        psId: null,
        psName: null,
        psLg: null,
        psLt: null,
        psType: null,
        psDosage: null,
        psEnergyConsumption: null,
        psElectricity: null,
        psWater: null,
        psWasteWater: null,
        psMasterPollution: null,
        psMasterPollutionDirection: null,
        psFlowChat: null,
        psStopProApplication: null,
        psStopProTime: null,
        psBoilerProTime: null,
        psBoilerType: null,
        epId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.psId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产工艺";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const psId = row.psId || this.ids
      getProcess(psId).then(response => {
        this.form = response.data;
        if(this.form.psWasteWater == 0 ){
          this.guolu = true;
        }
        this.form.psWasteWater= this.form.psWasteWater + "";
        this.open = true;
        this.title = "修改生产工艺";
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.psId != null) {
            updateProcess(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const username = this.$store.state.user.name;
            InfoItem(username).then(res => {
              if (res.hasOwnProperty("data")){
                this.form.epId = res.data.epId;
              }
             this.form.pc
              addProcess(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const psIds = row.psId || this.ids;
      this.$confirm('是否确认删除生产工艺编号为"' + psIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delProcess(psIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有生产工艺数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportProcess(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>

<style lang="postcss" scoped>
.fuliao{
  width: 100%;
  height: 30px;
  display: flex;
  justify-content:  space-around;
  margin-bottom: 10px;
}
.ful-input{
  width: 80%;
  display: flex;
  align-items: center;
  /*justify-content: space-between;*/
}

.ful-title{
  font-size:16px;
   color: #333;
  width: 50%;
  text-align: center;
  font-weight: 700;
}
.fuliao/deep/.el-input__inner{
  width: 100%;
  height: 30px !important;
}
.content-left {
  width: 30% !important;
  float: left;
}
.ful-btn{
  width: 20%;
  margin-left: 10px;
}

.ful-input-box{
  width: 50%;
  font-weight: bold;

  display: flex;
  align-items: center;
  justify-content: space-between;
}
.ful-input-box span{
  text-align: center;
  width: 40%;
  color: #333;
  display: inline-block;
}
.content-middle {
  width: 30%;
  float: left;
  margin-left: 3%;
}

.content-right {
  width: 30%;
  float: left;
  margin-left: 3%;
}

.content-address {
  display: flex;
  width: 100%;
}

.app-container-process {
  padding-left: 60px;
  padding-right: 60px;
}

.title2 {
  height: 40px;
  /*padding: 20px 0;*/

}

.title2 span {
  display: inline-block;
  border-radius: 50%;
  width: 15px;
  height: 15px;
  vertical-align: middle;
  margin-right: 10px;
  background-color: deepskyblue;
}

.avatar-uploader {
  width: 100%;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
}

.avatar {
  width: 128px;
  height: 128px;
  display: block;
}

.weidu {
  display: flex;
}
.edit-add{
  padding: 0;
  margin: 0;
  height: 60px;
  line-height: 30px;
  border-radius: 0;
}
</style>
